home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group97a.txt
/
000120_icon-group-sender _Mon Apr 21 10:25:00 1997.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Mon, 21 Apr 1997 13:23:14 MST
Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
id AA05093; Mon, 21 Apr 1997 13:23:14 -0700
Date: Mon, 21 Apr 1997 10:25:00 -0700
From: swampler@noao.edu (Steve Wampler)
Subject: Re: Comments on Icon Routine
To: icon-group@cs.arizona.edu
Message-Id: <swampler-9703211724.AA003615590@orpheus.gemini.edu>
In-Reply-To: <3358C3DD.23EE@airmail.net>
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
Content-Length: 2322
Nathanael Graham wrote:
> To fellow Iconers:
>
> This week at work I wanted to take documents created by MS-Word or
> WordPerfect and break down the 80+ character lines into lines of no more
> than 80 characters and I figured that this would be a perfect use for
> Icon. The routine did not take too long to code and, at the suggestion
> of a fellow worker, I generalized it to allow for lines of any specified
> length. Assuming the Icon routine is called truncate.icn/truncate.exe,
> one would type the following on the command line:
>
> truncate infile.txt outfile.txt 80
>
> This would:
>
> . Read in each record from infile.txt
> . After deleting leading spaces, it would write the record to
> outfile.txt, if the record length was 80 or less
> . If the record length was greater than 80, then it would "break up"
> the record on spaces, scanning for them from position 81 and going
> backwards. Once it found the space, it would write from the
> beginning of the record to the space and repeat the same
> algorithm starting with what was left of the record after the
> space. If no space was found in any record or piece thereof of
> length 80 or more, then the first 80 characters would be written,
> and the rest of the record would be handled per above.
>
I suspect you're going to see a lot of different ways to do this! Here's one
off the top of my head - I'm sure there are better ways.
procedure main(args)
in := open(args[1], "r") | stop("Cannot open ", args[1])
out := open(args[2], "w") | stop("Cannot open ", args[2])
rmargin := args[3] | 80
while produce(out, read(in), rmargin)
end
procedure produce(f, s, rm)
s := ltrim(s)
if *s <= rm
then write(f, s)
else {
reverse(s[1+:rm]) ? {
suffix := reverse(tab(upto(' \t'))) | ""
write(f, reverse(tab(0)))
produce(f, suffix||s[rm+1:0], rm)
}
}
return
end
procedure ltrim(s)
return s ?:= (tab(many(' ')), tab(0))
end
--
Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under AURA)]
O Sibile, si ergo, fortibus es inero.
Nobile, demis trux. Demis phulla causan dux.